package forum;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;

public class Post implements Serializable{

	private int postID;
	private int threadID;
	private int userID;
	private String content;
	private Timestamp createdOn;
	private Timestamp lasteditedOn;
	private User createdBy;
	private String editContent;
	
	public Post(){
		
	}
	public Post(int postID, int threadID, int userID, String content, Timestamp createdOn, Timestamp lasteditedOn){

		this.postID = postID;
		this.threadID = threadID;
		this.userID = userID;
		this.content = content;
		this.createdOn = createdOn;
		this.lasteditedOn = lasteditedOn;
		getUser();
	}
	
	public Post(int threadId, int createdByUserid, String content2) {
		this.postID = 0;
		this.threadID = threadId;
		this.userID = createdByUserid;
		this.content = content2;
		this.createdOn = new Timestamp(System.currentTimeMillis());
		this.lasteditedOn = new Timestamp(System.currentTimeMillis());
		getUser();
	}

	public synchronized void Create(){
		
		Connection conn = db_code.connect();
		try {
			//content = content.replaceAll("\n", "<br>");
			Statement stmt = conn.createStatement();
			String query = "INSERT INTO post (threadID, userID, content, createdOn , lasteditedOn)" +
					"VALUES(" + 
						threadID + "," + 
						userID + ",'" + 
						content + "','" + 
						createdOn + "','" + 
						lasteditedOn + "');";
			
			stmt.executeUpdate(query,Statement.RETURN_GENERATED_KEYS);
			
			ResultSet rs = stmt.getGeneratedKeys();
			if ( rs.next() ) {
			    int key = rs.getInt(1);
			    setPostID(key);
			}
			
		} catch (Exception e) {

			System.out.println("FAIL! INSERT!");
			
			e.printStackTrace();
		}
		db_code.disconnect(conn);
	}
	public static void editContent(int postID, String content){
		
		Connection conn = db_code.connect();
		try{
			
			Statement stmt = conn.createStatement();
			String query = "update post set content = '" + content + 
					"' , lasteditedOn='" + new Timestamp(System.currentTimeMillis()) +
					"' where postID = " + postID;
			stmt.execute(query);
			db_code.disconnect(conn);
		}catch (Exception e){
			db_code.disconnect(conn);
			System.out.println("failed to edit post");
			e.printStackTrace();
		}
		
	}
	
	public synchronized void Edit(){
		
		Connection conn = db_code.connect();
		try {
			Statement stmt = conn.createStatement();
			String query = "UPDATE post " +
					"SET " +
						"threadID = " + threadID + "," +
						"userID = " + userID + ","+
						"content = '" + content + "',"+
						"createdOn = '" + createdOn + "',"+
						"lasteditedOn = '" + lasteditedOn +"'"+
					"WHERE " +
						"postID = " + postID;
			
			stmt.execute(query);
			
		} catch (Exception e) {
			System.out.println("FAIL! UPDATE!");
			e.printStackTrace();
		}
		db_code.disconnect(conn);
	}
	
	public synchronized void Delete(){
		Connection conn = db_code.connect();
		try {
			Statement stmt = conn.createStatement();
			String query = "DELETE FROM post WHERE postID = " + postID;
			
			stmt.execute(query);
		} catch (Exception e) {
			System.out.println("FAIL! DELETE!");
			e.printStackTrace();
		}
		db_code.disconnect(conn);
	}
	
	
	public String toString(){
		return "postID: " + postID + 
				", threadID: " + threadID + 
				", userID: "  + userID + 
				", content: "  + content + 
				", createdOn: "  + createdOn + 
				", lasteditedOn: " + lasteditedOn;
	}
	
	public int getPostID() {
		return postID;
	}
	public void setPostID(int postID) {
		this.postID = postID;
	}
	public int getThreadID() {
		return threadID;
	}
	public void setThreadID(int threadID) {
		this.threadID = threadID;
	}
	public int getUserID() {
		return userID;
	}
	public void setUserID(int userID) {
		this.userID = userID;
	}
	public Timestamp getCreatedOn() {
		return createdOn;
	}
	public void setCreatedOn(Timestamp createdOn) {
		this.createdOn = createdOn;
	}
	public String getContent() {
		return content.replace("\n","<br>");
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Timestamp getLasteditedOn() {
		return lasteditedOn;
	}
	public void setLasteditedOn(Timestamp lasteditedOn) {
		this.lasteditedOn = lasteditedOn;
	}

	public User getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(User createdBy) {
		this.createdBy = createdBy;
	}
	private void getUser(){
		Connection conn = db_code.connect();
		try{
 			
			Statement stmt = conn.createStatement();
			ResultSet rs = null;
			
			String query;
			query = "SELECT * FROM user WHERE userID = '" + userID + "' LIMIT 1;";
			
			rs = stmt.executeQuery(query);
			
			while(rs.next()){
				this.createdBy  = new User(
						rs.getInt("userID"),
						rs.getInt("userlvl"),
						rs.getString("name"),
						rs.getString("email"),
						rs.getString("password"),
						rs.getTimestamp("joined"),
						rs.getInt("numberOfPosts")
						);
			}
			db_code.disconnect(conn);
 		}
		catch(SQLException e){
			db_code.disconnect(conn);
			System.out.println("SQL statement error!");
			e.printStackTrace();
		}
		
	}
	public String getEditContent() {
		editContent = content;
		return editContent;
	}
	public void setEditContent(String editContent) {
		this.editContent = editContent;
		this.content = editContent;
	}
}
